<?php
$host = 'localhost';
$user = 'your_user_name';
$password = 'your_password';
$dbanme = 'your_database_here';

$link1 = mysql_connect($host, $user, $password);
mysql_select_db('information_schema', $link1);

$result = mysql_query("SELECT *  FROM `information_schema`.`COLUMNS` WHERE `TABLE_SCHEMA` = '$dbname' AND `DATA_TYPE` in ('varchar', 'text')", $link1) or die(mysql_error($link1));

$link2 = mysql_connect($host, $user, $password);
mysql_select_db($dbname, $link2);
$success = $fail = array();
$tables = array();
while (($row = mysql_fetch_assoc($result)) != false){
	$tables[$row['TABLE_NAME']] = $row['TABLE_NAME'];
	$query = "ALTER TABLE `{$row['TABLE_NAME']}` MODIFY `{$row['COLUMN_NAME']}` {$row['COLUMN_TYPE']} CHARACTER SET utf8 COLLATE utf8_unicode_ci ";
	
	if ($row['IS_NULLABLE'] == 'YES'){
		$query .= 'NULL';
	} else {
		$query .= 'NOT NULL';
	}
	
	$res = mysql_query($query, $link2);
	if ($res){
		$success["{$row['TABLE_NAME']}.{$row['COLUMN_NAME']}"] = $query;
	} else {
		$fail["{$row['TABLE_NAME']}.{$row['COLUMN_NAME']}"] = mysql_error($link2);
	}
}

echo '<pre>', print_r(compact('success', 'fail'), true), '</pre>';

foreach ($tables as $table){
	mysql_query("ALTER TABLE `$table` DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci", $link2);
}

mysql_query("ALTER DATABASE `$dbname` DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci");

